﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FileCopy
{
    class CopyStatus
    {
        public string status;
        public int index;
        public int find;
        public int fail;
        public int total;
        public bool exists;
        public CopyStatus(string _status,int _index,int _find,int _fail,int _total,bool _exists)
        {
            status = _status;
            index = _index;
            find = _find;
            fail = _fail;
            total = _total;
            exists = _exists;
        }
    }
    class CopyFile
    {
        mainForm myForm;
        CopyStatus csStatus;
        string path;
        string ext;
        delegate void SetTextCallback(CopyStatus csStatus);//设置委托
        //初始化
        public CopyFile(mainForm _myForm,string _path,string _ext)
        {
            myForm = _myForm;
            path = _path;
            ext = _ext;
        }
        public void Copy()
        {
            int iFind;
            int iTotal;
            int iFail;
            try
            {
                string[] subdir=Directory.GetDirectories(path);
                string currentPath = System.IO.Directory.GetCurrentDirectory()+"\\file";
                iFind = 0;
                iTotal = myForm.dgvCopyList.Rows.Count;
                iFail = 0;
                //读取表格内第一个文件名
                for (int i = 0; i < iTotal; i++)
                {
                    int count=0;
                    //先在当前目录找
                    string filename = myForm.dgvCopyList.Rows[i].Cells["FileName"].Value.ToString() + ext ;
                    string filepath= path + "\\" + filename;
                    while (!File.Exists(filepath) && count < subdir.Length)
                    {
                        filepath=subdir[count]+"\\"+filename;
                        count++;
                    }
                    //如果找到了文件
                    if (File.Exists(filepath))
                    {
                        //复制文件到当前程序目录
                        if (!Directory.Exists(currentPath))
                        {
                            Directory.CreateDirectory(currentPath);
                        }
                        File.Copy(filepath,currentPath+"\\"+filename,true);
                        iFind++;
                        csStatus = new CopyStatus(filename, i, iFind, iFail, iTotal, true);
                        setWorkStatus(csStatus);

                    }
                    else
                    {
                        iFail++;
                        csStatus = new CopyStatus(filename, i, iFind, iFail, iTotal, false);
                        setWorkStatus(csStatus);
                    }
                }
                MessageBox.Show(iFind+"个文件已提取到" + currentPath,"提取完成");
            }
            catch (Exception ex)
            {
                csStatus = new CopyStatus("提取出错！" + ex.Message.ToString(), 0, 0, 0, 0, true);
                setWorkStatus(csStatus);
            }      
            
        }
        public void setWorkStatus(CopyStatus csStatus)
        {
            //来设置工作状态，解决多线程下不能设置问题
            try
            {
                if (myForm.InvokeRequired)
                {
                    SetTextCallback d = new SetTextCallback(setWorkStatus);
                    myForm.Invoke(d, new object[] { csStatus });

                }
                else
                {
                    if (csStatus.total == 0)
                    {
                        myForm.tsslStatus.Text = csStatus.status;
                        myForm.btCopyStart.Text = "开始提取";
                    }
                    else
                    {
                        myForm.tsslStatus.Text = csStatus.status + "已提取，完成" + (csStatus.find + csStatus.fail) + "/" + csStatus.total + "，缺失" + csStatus.fail + "个。";
                        //没有找到文件，就添加到未找到列表里面
                        if (!csStatus.exists)
                        {
                            myForm.tsslStatus.Text = csStatus.status + "未找到，完成" + (csStatus.find + csStatus.fail) + "/" + csStatus.total + "，缺失" + csStatus.fail + "个。";
                            string filename = myForm.dgvCopyList.Rows[csStatus.index].Cells["FileName"].Value.ToString();
                            string prop1 = myForm.dgvCopyList.Rows[csStatus.index].Cells["Prop1"].Value.ToString();
                            string prop2 = myForm.dgvCopyList.Rows[csStatus.index].Cells["Prop2"].Value.ToString();
                            string prop3 = myForm.dgvCopyList.Rows[csStatus.index].Cells["Prop3"].Value.ToString();
                            myForm.dgvCopyFail.Rows.Add(filename, prop1, prop2, prop3);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string info = ex.Message + ex.Data;

                MessageBox.Show(info);

            }

        }
    }
    
}
